<?php

namespace Modules\Edu\Http\Controllers\Front;

use App\Http\Controllers\LiveAdmin\LiveAdminController;
use App\Models\Live;
use App\Models\LiveAdmin;
use App\Services\LiveService;
use App\Services\ConfigService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;


class LiveController extends Controller
{

    public function index(Request $request)
    {
        if ($request->input('is_live')) {
            $lives = Live::where('config->is_live', true)->paginate();
        } else {
            $lives = Live::paginate();
        }

        return view('edu::live.index', compact('lives'));
    }

    public function show(Live $live){
        $config=$live->config;
        //测试
//        app(LiveAdminController::class)->setKickout(2,100,'dsfss');
        return view('edu::live.show',compact('config','live'));
    }

    //底部为向大直播间即站点直播

    public function push(LiveService $liveService, ConfigService $moduleConfigService)
    {
        $config = config('module.live');
        $stream = Str::random(10);
        $pushUrl = $liveService->pushUrl($config['push_url'], 'houdunren', $stream, $config['push_key']);

        $playUrls = $liveService->playUrl($config['play_url'], 'houdunren', $stream, $config['play_key']);

        $moduleConfigService->saveCurrentModuleConfig(['push' => $pushUrl, 'play' => $playUrls, 'is_live' => false]);

        $res = $liveService->notify(
            config('site.aliyun.accessKeyId'),
            config('site.aliyun.accessKeySecret'),
            $config['push_url'],
            route('edu.front.live.notify')
        );

        return back()->with('success', '直播地址已生成');
    }

    public function notify(Request $request, ConfigService $moduleConfigService)
    {
        switch ($request->input('action')) {
            case "publish":
                $moduleConfigService->saveCurrentModuleConfig(['is_live' => true]);
                break;

            case 'publish_done':
                $moduleConfigService->saveCurrentModuleConfig(['is_live' => false]);
                break;
        }
    }
}
